﻿using System.Collections.Generic;
using MySql.Data.MySqlClient;
using Portfolio.Models;

namespace Portfolio.Persistencia
{
    public class Fotos
    {
        public static IEnumerable<Foto> ListarFotos(int idEvento)
        {
            List<Foto> fotos = null;

            using (MySqlConnection conn = DBContext.GetInstance())
            {
                MySqlCommand command = new MySqlCommand()
                {
                    Connection = conn,
                    CommandType = System.Data.CommandType.Text,
                    CommandText = string.Format("SELECT * FROM Fotos WHERE IdEvento = {0}", idEvento)
                };

                conn.Open();

                try
                {
                    MySqlDataReader reader = command.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

                    command.Dispose();

                    if (reader.HasRows)
                    {
                        fotos = new List<Foto>();

                        while (reader.Read())
                        {
                            fotos.Add(new Foto()
                            {
                                Id = (int)reader["Id"],
                                IdEvento = (int)reader["IdEvento"],
                                NomeImagem = reader["NomeImagem"].ToString()
                            });
                        }
                    }

                    reader.Close();
                    reader.Dispose();
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }

            return fotos;
        }
    }
}